/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.deployment.manager;

import br.com.deployment.utilitario.EstadoCivil;
import br.com.deployment.utilitario.Sexo;
import br.com.deployment.utilitario.TipoLogradouro;
import br.com.deployment.utilitario.TipoEndereco;
import br.com.deployment.entidade.empresa.Cartorio;
import br.com.deployment.entidade.localidade.Endereco;
import br.com.deployment.entidade.humana.Titular;
import br.com.deployment.entidade.registroimovel.GeoRef;
import br.com.deployment.message.Msg;
import br.com.deployment.servico.CartorioServico;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;

/**
 *
 * @author elyjr
 */
@ManagedBean
@SessionScoped
public class ManagerCartorio implements Serializable {

   
    @EJB
    CartorioServico cartorioServico;
    
    private Cartorio cartorio;
    private Titular titular;
    private Endereco endereco;
    private Endereco endtitular;

    private List<GeoRef> lstGeoRef;


    @PostConstruct
    public void init() {
        cartorio = new Cartorio();
        endereco = new Endereco();
        titular = new Titular();
        endtitular = new Endereco();
         this.lstGeoRef = new ArrayList<GeoRef>();
        addGeoRef();
    }

    public void reinit() {
       init();
    }
    
    
    public List<GeoRef> getLstGeoRef() {
        return lstGeoRef;
    }

    public void setLstGeoRef(List<GeoRef> lstGeoRef) {
        this.lstGeoRef = lstGeoRef;
    }

    
    public Endereco getEndereco() {
        return endereco;
    }

    public void setEndereco(Endereco endereco) {
        this.endereco = endereco;
    }

    public Cartorio getCartorio() {
        return cartorio;
    }

    public void setCartorio(Cartorio cartorio) {
        this.cartorio = cartorio;
    }

    public Titular getTitular() {
        return titular;
    }

    public void setTitular(Titular titular) {
        this.titular = titular;
    }

    public Endereco getEndtitular() {
        return endtitular;
    }

    public void setEndtitular(Endereco endtitular) {
        this.endtitular = endtitular;
    }   


   public void salvar() {
       cartorio.setGeoAtuacao(lstGeoRef);
       cartorio.setEndereco(endereco);
       titular.setEndereco(endtitular);
       cartorio.setTitular(titular);
       cartorioServico.salvar(cartorio);
       reinit();
        Msg.messagemInfo("Cartorio salvo com sucesso ");
    }
   
    public void redirect(String string) {
        try {
            FacesContext context = FacesContext.getCurrentInstance();
            context.getExternalContext().redirect(string);
        } catch (IOException ex) {
            Logger.getLogger(ManagerCartorio.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    
       public SelectItem[] selectItemSexo() {
        SelectItem[] items = new SelectItem[Sexo.values().length];
        int i = 0;
        for (Sexo sexo : Sexo.values()) {
            items[i++] = new SelectItem(sexo);
        }
        return items;
    }

    public SelectItem[] selectItemEstadoCivil() {
        SelectItem[] items = new SelectItem[EstadoCivil.values().length];
        int i = 0;
        for (EstadoCivil estadoCivil : EstadoCivil.values()) {
            items[i++] = new SelectItem(estadoCivil);
        }
        return items;
    }

    public SelectItem[] selectItemTipoEndereco() {
        SelectItem[] items = new SelectItem[TipoEndereco.values().length];
        int i = 0;
        for (TipoEndereco tipo : TipoEndereco.values()) {
            items[i++] = new SelectItem(tipo);
        }
        return items;
    }

    public SelectItem[] selectItemTipoLogradouro() {
        SelectItem[] items = new SelectItem[TipoLogradouro.values().length];
        int i = 0;
        for (TipoLogradouro tipo : TipoLogradouro.values()) {
            items[i++] = new SelectItem(tipo);
        }
        return items;
    }


    public void clear() {
    }
    
    public void addGeoRef(){
          lstGeoRef.add(new GeoRef());
        
    }
    public void removeGeoRef(GeoRef gf){
        lstGeoRef.remove(gf);
    }

    
}